

// 165.比较版本号
class Solution {
public:
    int compareVersion(string version1, string version2) {
        // 查找.的位置进行比较
        int n = version1.size(), m = version2.size();
        int pos1 = 0, pos2 = 0;
        while (pos1 < n || pos2 < m) {
            int v1 = 0, v2 = 0;
            if (pos1 < n) 
            {
                int end1 = version1.find('.', pos1);
                if (end1 == string::npos) v1 = stoi(version1.substr(pos1));
                else v1 = stoi(version1.substr(pos1, end1 - pos1));
                pos1 = (end1 == string::npos ? n : end1) + 1;
            }
            if (pos2 < m) 
            {
                int end2 = version2.find('.', pos2);
                if (end2 == string::npos) v2 = stoi(version2.substr(pos2));
                else v2 = stoi(version2.substr(pos2, end2 - pos2));
                pos2 = (end2 == string::npos ? m : end2) + 1;
            }
            if (v1 < v2) return -1;
            else if (v1 > v2) return 1;
        }
        return 0;
    }
};